//
//  ZMyOrderDetailVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZMyOrderDetailVC.h"
#import "ZMyOrderDetailAddressInfoCell.h"//地址
#import "ZMyOrderShopInfoTopCell.h"//店铺
#import "ZMyOrderInfoCell.h"//配件订单

#import "ZMyOrderListOrderModel.h"

#import "ZMyOrderListOrderChangeView.h"
#import "ZMyOrderListOrderCancelView.h"

#import "BBuyNowShopStoreVC.h"//店铺

@interface ZMyOrderDetailVC () <UITableViewDataSource,UITableViewDelegate,ZMyOrderInfoCellDelegate,ZMyOrderListOrderChangeViewDelegate,ZMyOrderListOrderCancelViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZMyOrderListOrderModel *orderModel;
@property (nonatomic, strong) NSIndexPath *indexPathSelected;//被操作的位置
@end

@implementation ZMyOrderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"预订单详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    if (AGENT_LEVEL == 1) {
        self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    }else{
        self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    
    [self setupViewAction];
    
    [self requestData];
}

#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (AGENT_LEVEL == 1) {
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZMyOrderDetailAddressInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderDetailAddressInfoCell class])];
    [_tableView registerClass:[ZMyOrderShopInfoTopCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderShopInfoTopCell class])];
    [_tableView registerClass:[ZMyOrderInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderInfoCell class])];
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObjectIfNotNil:self.orderID forKey:@"id"];
    [NetWorkTool POST:GET_PARTS_ORDER_INFO param:dict success:^(id dic) {
        weakSelf.orderModel = [ZMyOrderListOrderModel mj_objectWithKeyValues:[dic objectForKeyNotNil:@"data"]];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.orderModel) {
        return 2 + self.orderModel.parts.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ZMyOrderDetailAddressInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderDetailAddressInfoCell class]) forIndexPath:indexPath];
        cell.orderModel = self.orderModel;
        return cell;
    }else if (indexPath.row == 1){
        ZMyOrderShopInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderShopInfoTopCell class]) forIndexPath:indexPath];
        cell.orderModel = self.orderModel;
        return cell;
    }else{
        ZMyOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderInfoCell class]) forIndexPath:indexPath];
        cell.isOrderDetail = YES;
        BShopPartModel *partModel = [self.orderModel.parts objectAtIndexSafe:indexPath.row - 2];
        cell.partModel = partModel;
        cell.indexPath = indexPath;
        cell.delegate = self;
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
         return UITableViewAutomaticDimension;
    }else if (indexPath.row == 1){
        return [ZMyOrderShopInfoTopCell cellHeight];
    }else{
        return [ZMyOrderInfoCell cellDetailHeight];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
//        BBuyNowShopStoreVC *vc = [BBuyNowShopStoreVC new];
//        vc.storeID = self.orderModel.Id;
//        vc.orderListModel = self.orderModel;
//        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - ZMyOrderInfoCellDelegate
- (void)orderInfoCellBtnChangeClick:(NSIndexPath *)indexPath{
    self.indexPathSelected = indexPath;
    ZMyOrderListOrderChangeView *viewChange = [[ZMyOrderListOrderChangeView alloc] init];
    viewChange.delegate = self;
    BShopPartModel *orderModel = [self.orderModel.parts objectAtIndexSafe:indexPath.row - 2];
    viewChange.orderModel = orderModel;
    [viewChange showView];
}
- (void)orderInfoCellBtnCancelClick:(NSIndexPath *)indexPath{
    self.indexPathSelected = indexPath;
    ZMyOrderListOrderCancelView *viewCancel = [[ZMyOrderListOrderCancelView alloc] init];
    viewCancel.delegate = self;
    BShopPartModel *orderModel = [self.orderModel.parts objectAtIndexSafe:indexPath.row - 2];
    viewCancel.orderModel = orderModel;
    [viewCancel showView];
}
#pragma mark - ZMyOrderListOrderCancelViewDelegate
- (void)orderCancelSuccess{
    //刷新cell
    [_tableView reloadRowsAtIndexPaths:@[self.indexPathSelected] withRowAnimation:UITableViewRowAnimationNone];
    [[NSNotificationCenter defaultCenter] postNotificationName:USER_MODIFY_ORDER_SUCCESS object:nil];
}
#pragma mark - ZMyOrderListOrderChangeViewDelegate
- (void)orderUpdateSuccess{
    //刷新cell
    [_tableView reloadRowsAtIndexPaths:@[self.indexPathSelected] withRowAnimation:UITableViewRowAnimationNone];
    [[NSNotificationCenter defaultCenter] postNotificationName:USER_MODIFY_ORDER_SUCCESS object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
